package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.entity.MqLocationData;
import com.wtwd.campus.entity.MqMessage;
import com.wtwd.campus.entity.MqWifiData;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.service.WarningRecordService;
import com.wtwd.campus.utils.GPSUtils;
import com.wtwd.campus.utils.GaoDeUtil;
import com.wtwd.campus.utils.ReceiverUtils;
import com.wtwd.campus.utils.Vizhi.VizhiService;
import com.wtwd.campus.utils.Vizhi.entity.Address;
import com.wtwd.campus.utils.Vizhi.entity.Point;
import com.wtwd.campus.utils.Vizhi.entity.Position;
import com.wtwd.campus.utils.Vizhi.entity.vo.VizhiVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@MsgType(value = Constants.WIFIMAC)
public class WifiMacReceiverHandler implements IReceiverHandler {
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private WarningRecordService warningRecordService;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private DeviceLogService deviceLogService;

    @Resource
    private VizhiService vizhiService;

    public static final Long EXPIRATION_TIME = 1720886400000L;
    public static List<String> imeiGlobal = new ArrayList<>(Arrays.asList("358317101254130", "358317101254148", "358317101254155", "358317101254163"));

    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqWifiData mqWifiData = (MqWifiData) mqMessage.getObject();
        String imei = mqWifiData.getImei();
        String commandNumber = mqWifiData.getCommandNumber();
        //处理指令回复的时间
        String date = mqWifiData.getDeviceDate();
        String time1 = mqWifiData.getDeviceTime();
        Long time = getTime(date, time1);
        DeviceInfo deviceInfo = deviceInfoService.getDeviceByImei(imei);
        Integer battery = mqWifiData.getBattery();
        Integer stepNum = mqWifiData.getStepNum();
        String wifiInfo = mqWifiData.getWifiInfo();
        if (deviceInfo != null) {
            MqLocationData mqLocationData = new MqLocationData();
            VizhiVo wifiAddress = vizhiService.intelligentHardwareLocation(imei, wifiInfo,null);
            if (wifiAddress==null){
                return;
            }
            Address address = wifiAddress.getLocation().getAddress();
            if (address!=null&& StringUtils.isNotBlank(address.getName())){
                mqLocationData.setAddress(address.getName());
            }
            Position position = wifiAddress.getLocation().getPosition();
            Point point = position.getPoint();
            String[] split = {String.valueOf(point.getLongitude()),String.valueOf(point.getLatitude())};
            double[] lgtLat = GPSUtils.gcj02_To_Gps84(Double.parseDouble(split[1]), Double.parseDouble(split[0]));
            if (imeiGlobal.contains(imei) && EXPIRATION_TIME > new Date().getTime()) {
                mqLocationData.setLongitude(Double.parseDouble(split[0]));
                mqLocationData.setLatitude(Double.parseDouble(split[1]));
            } else {
                mqLocationData.setLongitude(lgtLat[1]);
                mqLocationData.setLatitude(lgtLat[0]);
            }
            //存储定位信息以及围栏告警
            deviceInfo = receiverUtils.dealLocation(mqLocationData, time, deviceInfo, 2);
            //修改设备信息
            deviceInfo.setLastUpTime(time);
            deviceInfo.setBattery(battery);
            deviceInfoService.modifyDeviceInfo(deviceInfo);
//            if (battery < 20) {
//                //存储低电量告警
//                WarningRecord record = new WarningRecord();
//                record.setDevice_id(deviceInfo.getDeviceId());
//                record.setAddress(deviceInfo.getAddress());
//                record.setWarning_time(time);
//                record.setWarning_type(Constants.LOW_BATTERY_WARNING_TYPE);
//                warningRecordService.addWarningRecord(record);
//            }
            //存储设备接收日志
            DeviceLog deviceLog = new DeviceLog();
            deviceLog.setSendUser(Constants.DEVICE_USER);
            deviceLog.setImei(imei);
            deviceLog.setBigType(Constants.WIFIMAC);
            JSONObject contant = new JSONObject();
            contant.put("设备号", imei);
            contant.put("电量", mqWifiData.getBattery());
            contant.put("步数", mqWifiData.getStepNum());
            contant.put("Wif信息", mqWifiData.getWifiInfo());
            contant.put("步数", mqWifiData.getStepNum());
            contant.put("时间", date);
            contant.put("wifi详细信息:mac地址,信号强度,wifi名称|mac地址,信号强度,wifi名称", wifiInfo);
            deviceLog.setCmd(contant.toJSONString());
            deviceLog.setSendTime(System.currentTimeMillis());
            deviceLog.setCommandNumber(commandNumber);
            deviceLog.setState(1);
            deviceLogService.addDeviceLog(deviceLog);
        } else {
            saveUnInStorage(imei);
            receiverUtils.cloaseChannel(imei);
        }
    }

    public static void main(String[] args) {
        JSONObject wifiAddress = GaoDeUtil.getWifiAddress("4e:77:66:49:18:8a,-37|00:4b:f3:a8:3e:9a,-89|02:4b:f3:a8:3e:9a,-89|e2:94:c7:7f:30:57,-90|ac:0b:fb:25:78:59,-92", "868976030559126");
        System.out.println(wifiAddress.toJSONString());
    }
}
